<template>
    <el-main>
        <el-form :model="searchModel" ref="searchForm" label-width="80px" :inline="true" size="small">
            <el-form-item>
                <el-select placeholder="请选择员工" clearable v-model="selectUserId" v-if="isAdmin === 1">
                    <el-option v-for="item in employeeOptions" :key="item.id" :label="item.username" :value="item.id">
                    </el-option>
                </el-select>
                <el-date-picker v-model="year" type="date" placeholder="请选择日期" format="yyyy/MM/dd"
                    value-format="yyyy-MM-dd hh:mm:ss" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" plain icon="el-icon-search" @click="search(pageNo, pageSize)">查询
                </el-button>
                <el-button icon="el-icon-refresh-right" @click="resetValue">重置</el-button>
                <el-button type="success" plain icon="el-icon-plus" @click="openAddWindow()"
                    :disabled="isAdmin !== 1">新增</el-button>
            </el-form-item>
        </el-form>
        <!-- 数据表格 -->
        <el-table :data="salaryList" :height="tableHeight" border stripe style="width: 100%; margin-bottom: 10px">
            <el-table-column prop="id" label="员工编号" width="100" align="center"></el-table-column>
            <el-table-column prop="userName" label="员工名称"></el-table-column>
            <el-table-column prop="deptName" label="所属部门" width="200"></el-table-column>
            <el-table-column prop="dateMonth" label="年月" width="200"></el-table-column>
            <el-table-column prop="salaryTime" label="发放时间" width="200"></el-table-column>
            <el-table-column prop="salary" label="发放薪资"></el-table-column>
            <el-table-column prop="notes" label="备注说明"></el-table-column>
            <el-table-column label="操作" align="center" width="240">
                <template slot-scope="scope">
                    <el-button icon="el-icon-edit" type="primary" plain size="small" @click="handleEdit(scope.row)"
                        :disabled="isAdmin !== 1">编辑
                    </el-button>
                    <el-button icon="el-icon-delete" type="danger" plain size="small" @click="handleDelete(scope.row)"
                        :disabled="isAdmin !== 1">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页工具栏 -->
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="pageNo" :page-sizes="[10, 20, 30, 40, 50]" :page-size="10"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
        <!-- 添加和修改员工薪资窗口 -->
        <system-dialog :title="salaryDialog.title" :visible="salaryDialog.visible" :width="salaryDialog.width"
            :height="salaryDialog.height" @onClose="onClose" @onConfirm="onConfirm">
            <div slot="content">
                <el-form :model="salary" ref="salaryForm" :rules="rules" label-width="80px" :inline="false"
                    size="small">
                    <el-form-item label="员工姓名" prop="userId">
                        <el-select placeholder="请选择员工" clearable v-model="salary.userId">
                            <el-option v-for="item in employeeOptions" :key="item.id" :label="item.username"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="所属部门" prop="deptId">
                        <el-select placeholder="请选择部门" clearable v-model="salary.deptId">
                            <el-option v-for="item in deptions" :key="item.id" :label="item.departmentName"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="发放年月" prop="dateMonth">
                        <el-date-picker v-model="salary.dateMonth" type="date" placeholder="请选择日期" format="yyyy/MM/dd"
                            value-format="yyyy-MM-dd hh:mm:ss" />
                    </el-form-item>8
                    <el-form-item label="发放时间" prop="salaryTime">
                        <el-date-picker v-model="salary.salaryTime" type="date" placeholder="请选择日期"
                            format="yyyy/MM/dd hh:mm:ss" value-format="yyyy-MM-dd hh:mm:ss" />
                    </el-form-item>
                    <el-form-item label="发放薪资" prop="salary">
                        <el-input type="text" v-model="salary.salary"></el-input>
                    </el-form-item>
                    <el-form-item label="备注说明" prop="notes">
                        <el-input type="textarea" v-model="salary.notes" :rows="5"></el-input>
                    </el-form-item>
                </el-form>
            </div>
        </system-dialog>
    </el-main>
</template>

<script>
import departmentApi from "@/api/department";
//导入对话框组件
import salaryApi from "@/api/salary";
import userApi from "@/api/user";
import SystemDialog from "@/components/system/SystemDialog.vue";
import { mapGetters } from 'vuex';
export default {
    name: "salaryList",
    components: {
        SystemDialog,
    },
    data() {
        return {
            selectUserId: null,
            year: null,
            //查询条件
            searchModel: {
                pageNo: 1,
                pageSize: 10,
                time: null,
                userIds: []
            },
            rules: {
                userId: [
                    { required: true, trigger: "blur", message: "请输入员工薪资姓名" },
                ],
                deptId: [
                    { required: true, trigger: "blur", message: "请输入部门名称" },
                ],
                dateMonth: [
                    { required: true, trigger: "blur", message: "请输入发放年月" },
                ],
                salaryTime: [
                    { required: true, trigger: "blur", message: "请输入发放时间" },
                ],
                salary: [
                    { required: true, trigger: "blur", message: "请输入薪资" },
                ],
            },
            //添加和修改员工薪资窗口属性
            salaryDialog: {
                title: "",
                visible: false,
                height: 230,
                width: 500,
            },
            // 对象
            salary: {
                id: "",
                userId: null,
                userName: "",
                employee: "",
                deptId: null,
                departmentName: "",
                dateMonth: "",
                salaryTime: "",
                notes: "",
                salary: "",
                createUser: this.$store.getters.userId,
            },
            salaryList: [], //数据列表
            employeeOptions: [],
            deptions: [],
            tableHeight: 0, //表格高度
            pageNo: 1, //当前页码
            pageSize: 10, //每页显示数量
            total: 0, //总数量
        };
    },
    methods: {
        /**
         * 查询
         */
        async search(pageNo = 1, pageSize = 10) {
            this.searchModel.pageNo = pageNo; //当前页码
            this.searchModel.pageSize = pageSize; //每页显示数量
            if (this.selectUserId != null) {
                this.searchModel.userIds = [this.selectUserId]
            }
            if (this.year != null) {
                this.searchModel.time = this.year
            }
            //发送查询请求
            let res = await salaryApi.getsalaryListApi(this.searchModel);
            //执行成功
            if (res.success) {
                //员工薪资列表
                this.salaryList = res.data.records;
                //总数量
                this.total = res.data.total;
            }
        },
        /**
         * 获取员工薪资名单
         */
        async getStaff() {
            //发送查询请求
            let res = await userApi.getUserAllList({});
            //执行成功
            if (res.success) {
                console.log(res)
                //员工列表
                this.employeeOptions = res.data;
            }
        },
        /**
* 获取部门列表
*/
        async getDept() {
            //发送查询请求
            let res = await departmentApi.getDeptList({});
            //执行成功
            if (res.success) {
                console.log(res)
                //员工部门列表
                this.deptions = res.data;
            }
        },
        /**
         * 重置查询条件
         */
        resetValue() {
            //清空查询条件
            // console.log("start", this.searchModel);
            this.selectUserId = null;
            this.year = null;
            this.searchModel.time = null;
            this.searchModel.userIds = null;
            //重新查询
            // console.log("end", this.searchModel);
            this.search();
        },
        /**
         * 当每页数量发生变化时触发该事件
         */
        handleSizeChange(size) {
            this.pageSize = size; //将每页显示的数量交给成员变量
            this.search(this.pageNo, size);
        },
        /**
         * 当页码发生变化时触发该事件
         */
        handleCurrentChange(page) {
            this.pageNo = page;
            //调用查询方法
            this.search(page, this.pageSize);
        },
        /**
         * 打开添加窗口
         */
        openAddWindow() {
            //清空表单数据
            this.$resetForm("salaryForm", this.salary);
            this.salaryDialog.title = "新增薪资信息"; //设置窗口标题
            this.salaryDialog.visible = true; //显示窗口
        },
        /**
         * 窗口取消事件
         */
        onClose() {
            this.salaryDialog.visible = false;
        },
        handleEdit(row) {
            //数据回显
            this.$objCopy(row, this.salary); //将当前编辑的数据复制到salary对象中
            //设置窗口标题
            this.salaryDialog.title = "编辑薪资信息";
            //显示编辑员工薪资窗口
            this.salaryDialog.visible = true;
        },

        /**
            * 新增或编辑确认事件
            */
        onConfirm() {
            this.$refs.salaryForm.validate(async (valid) => {
                if (valid) {
                    let res = null;
                    //判断用户ID是否为空
                    res = await salaryApi.addSalary(this.salary);
                    //判断是否成功
                    if (res.success) {
                        this.search();
                        this.$message.success(res.message);
                        //关闭窗口
                    } else {
                        this.$message.error(res.message);
                    }
                    this.salaryDialog.visible = false;
                    this.search();
                }
            });
        },
        /**
       * 删除
        */
        async handleDelete(row) {
            //查询部门下是否存在子部门或用户
            let res = await salaryApi.deleteSalary({ mainIds: [row.id] });
            //判断是否可以删除
            if (res.success) {
                //成功提示
                this.$message.success(res.message);
                //刷新
                this.search();
            } else {
                //失败提示
                this.$message.error(res.message);
            }
        }


    },
    computed: {
        ...mapGetters([
            'name',
            'avatar',
            'roles',
            'isAdmin'
        ])
    },
    created() {
        //调用查询员工薪资列表的方法
        this.search();
    },
    mounted() {
        this.$nextTick(() => {
            this.getStaff();
            this.getDept();
            this.tableHeight = window.innerHeight - 220;
        });
    },
};
</script>

<style></style>